// pages/index/shopthress/shopthress.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    skulist: [],
    pageEnd: false,
    currentTab: 0,
    offset: 0,
    length: 20,
    pagenow: 1,
    tips: false,
    myinfo: '',
    valu1:'',
    valu2:'',
    bacslival:false,
    pricesdata:false,
    pricetext:''
  },
  obj: [],

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)
    this.setData({
      id: options.id

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  requestnav1: function () {
    let offset = this.data.skulist.length;
    app.func.postPromise('/categorySku', {
      id: this.data.id,
      offset: offset,
      length: this.data.length,
      sort: 0,
      
    }).then(([code, res]) => {
      console.log(res)
      let prolist = this.data.skulist;
      if (offset == 0 && res.data.length === 0) {
        app.func.toastPromise('该分类下暂无商品');
      } else {
        prolist = prolist.concat(res.data)
        this.setData({
          skulist: prolist,
          pageEnd: false,
          offset,
        });
        wx.hideLoading()
        wx.setNavigationBarTitle({
          title: res.name
        });
      }
      if (this.data.offset != 0 && res.data.length == 0) {
        this.setData({
          tips: true
        })
      }
    });
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestnav1()
    this.myinfos()
  },

  myinfos() {
    app.func.getPromise('/my/info')
      .then(([code, res]) => {
        this.setData({
          myinfo: res.message
        });
      })
  },
  //添加优选
  addChoose: function (e) {
    console.log(e)
    let skulist = this.data.skulist;
    let id = this.data.id;
    console.log(id)
    let index = e.currentTarget.dataset.index;
    app.func.getPromise('/addcart/' + id)
      .then(([code, res]) => {
        if (code == 200) {
          app.func.toastPromise('成功加入优选', 'none', 500);
          skulist[index].add_cart = 1
          this.setData({
            skulist,
          })
        } else {
          app.func.toastPromise(res.message);
        }
      })
  },
  //移除优选
  clearChoose: function (e) {
    let skulist = this.data.skulist
    let id = this.data.id;
    let index = e.currentTarget.dataset.index;
    app.func.getPromise('/delcart/' + id)
      .then(([code, res]) => {
        if (code == 200) {
          app.func.toastPromise('已移除优选', 'none', 500);
          skulist[index].add_cart = 0;
          this.setData({
            skulist,
          })
        } else {
          app.func.toastPromise(res.message);
        }
      })
  },
  skuIntro: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/productdetail/product-description/product-description?id=' + id
    })
  },
  productdetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/productdetail/productdetail?id=' + id
    })
  },
  login: function () {
    // console.log('登录');
    wx.switchTab({
      url: '/pages/about/about'
    })
  },

  wxSearchInput(e){
    let avalues = e.detail.value;
    let avalue= avalues.replace(/\D/g, '')
    this.setData({
      valu1:avalue,
    })
    if(this.data.valu1.length!=0 &&this.data.valu2.length!=0){
      this.setData({
        bacslival:true
      })
    }else{
      this.setData({
        bacslival:false
      })
    }
  
  },
  wxSearchInput1(e){
    let avalues = e.detail.value;
    let avalue= avalues.replace(/\D/g, '')
    this.setData({
      valu2:avalue,
    })
    if(this.data.valu1.length!=0 &&this.data.valu2.length!=0){
      this.setData({
        bacslival:true
      })
    }else{
      this.setData({
        bacslival:false
      })
    }

  },
  sumbitval(){
    let avlout =Number(this.data.valu1);
    let bvlout=Number(this.data.valu2);
    
    if(avlout >bvlout || avlout==bvlout){
      wx.showToast({
        title:'最高价格不能少于最低价格',
        icon:'none',
        duration:1000
      })
      return;
    }else{
      app.func.api.WestoreInfo.filtratePriceGoods({start:avlout, end:bvlout,name:'',category_id:this.data.id})
      .then(r => {
          console.log(r);
          if(r.success==true){
            this.setData({
              pricesdata:true,
              pricetext:r.data
            })
          }
          if(r.data.length==0){
            wx.showToast({
              title: '该价格区间无商品',
              icon:'none'
            })
          }
      })
    }
  },
  nosumber(){
    wx.showToast({
      title:'请选择价格区间',
      icon:'none',
      duration:1000
    })
  },
  clealprice(){
    this.setData({
      pricesdata:false
    })
  },

  addonshopbuy: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/cartshop/payif/payif?skuid=' + id
    })
  },

  attestation: function () {
    wx.navigateTo({
      url: '/pages/auth/preattestation/preattestation'
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    if (this.data.tips) {
      this.setData({
        tips: true
      })
    } else {
      wx.showLoading({
        title: '加载更多',
        mask: true
      });
      this.requestnav1();

    }


  },
//添加购物车
  addcart: function (e) {
    let skulist = this.data.skulist;
    let id = e.currentTarget.dataset.id;
    console.log(id)
    let index = e.currentTarget.dataset.index;
    app.func.postPromise('/addcartv2', {
      pro_id: id,
      count: 1
    }).then(([code, res]) => {
      console.log(res);
      if (code == 200) {
        wx.showToast({
          title: '加入购物车成功',
          icon: 'success',
          mask: true,
          duration: 1000

        })
        skulist[index].add_cart = 1
        this.setData({
          skulist,
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none',
          mask: true
        })
      }
    })
  },

})